﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using System.IO;

using CE.Global;
using CE.Promotion;
using CE.Database;
using CE.Configuration;

public partial class worldcup_email_verification : System.Web.UI.Page
{
    static readonly string PasswordHash = "P@@Sw0rd";
    static readonly string SaltKey = "G1u@yG1uay&KEY";
    static readonly string VIKey = "@g9l8u7a6y524680";

    protected override void InitializeCulture()
    {
        System.Web.HttpContext.Current.Session["Language"] = "th-TH";
        if (Request.QueryString["ln"] != null && Request.QueryString["ln"].ToString() != "" && Request.QueryString["ln"].ToString() == "1")
        {
            System.Web.HttpContext.Current.Session["Language"] = "en-US";
        }
        String selectedCulture = SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string _strWCActive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsWCActive");
        if (_strWCActive == "0")
        {
            if (Session[CE.Global.General.Guest] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
        }
        LiteralControl newln = new LiteralControl(Environment.NewLine);
        HtmlMeta language = new HtmlMeta();
        language.Name = "language";
        language.Content = Resources.Title.PageLanguage;

        //HtmlMeta keywords = new HtmlMeta();
        //keywords.Name = "keywords";
        //keywords.Content = Resources.Football.MetaKeyword;
        //HtmlMeta description = new HtmlMeta();
        //description.Name = "description";
        //description.Content = Resources.Football.MetaDescription;

        Page.Header.Title = Resources.Football.WCEmailPageTitle;
        if (!Page.IsPostBack)
        {
            
            lblTitle.Text = Resources.Football.WCTitle;
            imgPrizes.ImageUrl = Resources.Football.ImgPrize;
            lblPrizes.Text = Resources.Football.Prizes;
            hplHome.NavigateUrl = Resources.Football.HomeURL;
            hplGotoHome.NavigateUrl = Resources.Football.HomeURL;
            lblAlertTitle.Text = Resources.Football.Congratulations;
            lblTitleMessage.Text = Resources.Football.EmailVerified;
            lblVerifyMessage.Text = Resources.Football.VerifiedMessage;

            if (Request.QueryString["wcid"] != null && Request.QueryString["wcid"].ToString() != "")
            {
                Int64 _wcuserid = Convert.ToInt64(Decrypt(Request.QueryString["wcid"].ToString().Replace(' ','+')));
                if (_wcuserid > 0)
                {
                    WorldCupPromotionDB _worldcupPromotionDb = new WorldCupPromotionDB();
                    Int64 _resulr = _worldcupPromotionDb.VerifiedDate(_wcuserid);
                    //if (_resulr == -1)
                    //{
                    //    divAlert.Attributes.Add("class", "alert-error");
                    //    lblTitleMessage.Text = "";//"Your Email Verification has been fail.";
                    //    lblVerifyMessage.Text = "";//"Your Email has not validate";
                    //    lblAlertTitle.Text = Resources.Football.Sorry;
                    //}
                }
                //else
                //{
                //    divAlert.Attributes.Add("class", "alert-error");
                //    lblTitleMessage.Text = "";//"Your Email Verification has been fail.";
                //    lblVerifyMessage.Text = "";//"Your Email has not validate";
                //    lblAlertTitle.Text = Resources.Football.Sorry; //"Sorry!";
                //}
            }
            //else
            //{
            //    divAlert.Attributes.Add("class", "alert-error");
            //    lblTitleMessage.Text = "";//"Your Email Verification has been fail.";
            //    lblVerifyMessage.Text = "";// "Your Email has not validate";
            //    lblAlertTitle.Text = Resources.Football.Sorry; //"Sorry!";
            //}

        }

    }

    public static string Decrypt(string encryptedText)
    {
        try
        {
            byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
            byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, System.Text.Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };

            var decryptor = symmetricKey.CreateDecryptor(keyBytes, System.Text.Encoding.ASCII.GetBytes(VIKey));
            var memoryStream = new MemoryStream(cipherTextBytes);
            var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];

            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return System.Text.Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
        }
        catch (Exception ex)
        {
            return "-1";
        }
    }

    public static String SetCulture()
    {
        String selectedCulture = string.Empty;
        if (System.Web.HttpContext.Current.Session["Language"] != null)
        {
            selectedCulture = System.Web.HttpContext.Current.Session["Language"].ToString();
        }
        else
        {
            selectedCulture = ((System.Web.Configuration.GlobalizationSection)(System.Configuration.ConfigurationSettings.GetConfig("system.web/globalization"))).UICulture;
            System.Web.HttpContext.Current.Session["Language"] = selectedCulture;
        }

        Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedCulture);

        return selectedCulture;
    }


}
